package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiDatabaseIncrementScriptBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiDatabaseIncrementScriptDao;
import com.cw.jeeyt.dao.model.api.ApiDatabaseIncrementScriptDo;
import com.cw.jeeyt.service.intf.api.ApiDatabaseIncrementScriptService;
import com.cw.jeeyt.service.converter.api.ApiDatabaseIncrementScriptConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库增量脚本业务实现类
 *
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiDatabaseIncrementScriptServiceImpl implements ApiDatabaseIncrementScriptService {

    private static final Logger logger = LoggerFactory.getLogger(ApiDatabaseIncrementScriptServiceImpl.class);
    @Autowired
    private ApiDatabaseIncrementScriptDao apiDatabaseIncrementScriptDao;

    /**
     * 查询所有数据库增量脚本
     */
    @Override
    public List<ApiDatabaseIncrementScriptBo> queryApiDatabaseIncrementScriptList() throws Exception {
        return ApiDatabaseIncrementScriptConverter.doList2BoList(
            apiDatabaseIncrementScriptDao.queryApiDatabaseIncrementScriptList());
    }

    /**
     * 分页查询数据库增量脚本
     *
     * @param apiDatabaseIncrementScriptBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiDatabaseIncrementScriptBo> queryByPage(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo,
        Integer startNum, Integer pageSize, Order order) throws Exception {
        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return null;
        }
        PageInfo<ApiDatabaseIncrementScriptDo> pageInfo = apiDatabaseIncrementScriptDao.queryByPage(
            ApiDatabaseIncrementScriptConverter.bo2Do(apiDatabaseIncrementScriptBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ApiDatabaseIncrementScriptConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询数据库增量脚本信息
     *
     * @return
     */
    @Override
    public ApiDatabaseIncrementScriptBo getApiDatabaseIncrementScriptById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiDatabaseIncrementScriptConverter.do2Bo(apiDatabaseIncrementScriptDao.getById(id));
    }

    /**
     * 新增数据库增量脚本
     *
     * @param apiDatabaseIncrementScriptBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo) throws Exception {

        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseIncrementScriptDo apiDatabaseIncrementScript = ApiDatabaseIncrementScriptConverter.bo2Do(
            apiDatabaseIncrementScriptBo);
        apiDatabaseIncrementScript.setCreateBy(userBo.getId());
        apiDatabaseIncrementScript.setCreateDate(new Date());
        apiDatabaseIncrementScript.setUpdateBy(userBo.getId());
        apiDatabaseIncrementScript.setUpdateDate(new Date());
        Integer result = apiDatabaseIncrementScriptDao.insert(apiDatabaseIncrementScript);
        return result;
    }

    /**
     * 新增数据库增量脚本不为空的字段
     *
     * @param apiDatabaseIncrementScriptBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo) throws Exception {

        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseIncrementScriptDo apiDatabaseIncrementScript = ApiDatabaseIncrementScriptConverter.bo2Do(
            apiDatabaseIncrementScriptBo);
        apiDatabaseIncrementScript.setCreateBy(userBo.getId());
        apiDatabaseIncrementScript.setCreateDate(new Date());
        apiDatabaseIncrementScript.setUpdateBy(userBo.getId());
        apiDatabaseIncrementScript.setUpdateDate(new Date());
        Integer result = apiDatabaseIncrementScriptDao.insertSelective(apiDatabaseIncrementScript);
        return result;
    }

    /**
     * 更新数据库增量脚本
     *
     * @param apiDatabaseIncrementScriptBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo) throws Exception {

        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseIncrementScriptDo apiDatabaseIncrementScript = ApiDatabaseIncrementScriptConverter.bo2Do(
            apiDatabaseIncrementScriptBo);
        apiDatabaseIncrementScript.setUpdateBy(userBo.getId());
        apiDatabaseIncrementScript.setUpdateDate(new Date());

        apiDatabaseIncrementScriptDao.update(apiDatabaseIncrementScript);
    }

    /**
     * 更新数据库增量脚本不为空的字段
     *
     * @param apiDatabaseIncrementScriptBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo) throws Exception {

        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiDatabaseIncrementScriptDo apiDatabaseIncrementScript = ApiDatabaseIncrementScriptConverter.bo2Do(
            apiDatabaseIncrementScriptBo);
        apiDatabaseIncrementScript.setUpdateBy(userBo.getId());
        apiDatabaseIncrementScript.setUpdateDate(new Date());

        apiDatabaseIncrementScriptDao.updateSelective(apiDatabaseIncrementScript);
    }

    /**
     * 删除数据库增量脚本
     *
     * @param apiDatabaseIncrementScriptBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiDatabaseIncrementScriptBo apiDatabaseIncrementScriptBo) throws Exception {

        if (apiDatabaseIncrementScriptBo == null) {
            logger.error("数据库增量脚本为空");
            return;
        }
        apiDatabaseIncrementScriptDao.delete(ApiDatabaseIncrementScriptConverter.bo2Do(apiDatabaseIncrementScriptBo));
    }
}
